<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\BillLogsMerchant;
use App\Models\Admin\Users;
use App\Models\Admin\BillLogsFIshmonger;
use App\Models\Admin\BillLogsPool;
use App\Models\Admin\Banners;
use App\Models\Config;
use Illuminate\Http\Request;
use App\Tools\Image;
use App\Tools\Upload;

class ConfigController extends BaseController
{

    /**
     * 基础配置
     * @param Request $request
     */
    public function getList(Request $request)
    {
        $params = $_GET;
        $params['keywords'] = $request->input('keywords', "");
        $params['limit'] = $request->input('limit', $this->limit_arr[0]);
        $list = Config::getList($params);
        $data['items'] = $list;
        $data['keywords'] = "keywords={$params['keywords']}";
        return view('Admin/Config/get_list', $data);
    }

    /**
     * 添加配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if ($request->method() == "GET") return view('Admin/Config/add');
        $request->validate([
            'name' => 'required',
            'key' => 'required',
            'value' => 'required',
        ]);
        $params = $_POST;

        $save = Config::add($params);
        return response()->json($save);
    }

    /**
     * 修改配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {

        if ($request->method() == "GET") {
            $request->validate([
                'id' => 'required',
            ]);
            $params = $_GET;
            $data['info'] = Config::getInfo($params);
            return view('Admin/Config/edit', $data);
        }

        $request->validate([
            'id' => 'required',
            'name' => 'required',
            'value' => 'required',
        ]);
        $params = $_POST;

        $save = Config::edit($params);
        return response()->json($save);
    }

    /**
     * 删除配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $request->validate([
            'id' => 'required',
        ]);
        $params = $_POST;
        $save = Config::del($params);
        return response()->json($save);
    }

}